<?php

declare (strict_types=1);

namespace App\Application\Admin\Model;

use App\Application\Admin\Service\AccessService;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $meituan_id
 * @property string $appkey
 * @property string $secret
 * @property int $bid
 * @property string $auth_code
 * @property int $store_id
 * @property int $admin_id
 * @property string $open_shop_uuid
 * @property string $app_shop_account
 * @property string $app_shop_accountname
 * @property string $shop_id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \Hyperf\Database\Model\Collection|self[] $children
 */
class Meituan extends Model
{
    use SoftDeletes;

    protected string $primaryKey = 'meituan_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'meituan';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = ['meituan_id', 'appkey', 'secret', 'bid', 'app_shop_account','app_shop_accountname', 'store_id','shop_id','admin_id'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'meituan_id' => 'integer',
        'appkey' => 'string',
        'secret' => 'string',
        'expires_in' => 'integer',
        'bid' => 'string',
        'store_id' => 'integer',
        'admin_id' => 'integer',
        'app_shop_account'=>'string',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];


    /**
     * 创建门店
     *
     * @return bool
     * @throws ErrorException
     */
    static function createMeituan(int $meituan_id, array $meituan = []): self
    {
        Db::beginTransaction();
        $meituan = self::updateOrCreate(['meituan_id' => $meituan_id], $meituan);
        Db::commit();
        return $meituan;

    }

}